home *** CD-ROM | disk | FTP | other *** search
- /* XOFWD.C Mail forwarding routines for XOBBS. Jim Durham, W2XO 4-7-89 */
- /* Version 1.3 */
- /* Code released to the amateur radio community */
-
- #include "xobbs.h"
-
- char bbsname[15][7]; /*bbs name array*/
- char *connectline; /*pointer to connect line*/
- char *recvline[5]; /*pointer to reply lines*/
- char *sendline[5]; /*pointer to send lines*/
- int bbsindex; /*number of bbs we are talking*/
- char device[5];
- int flag;
- int bbscount=0; /*how many bbses in array*/
- jmp_buf env; /* setjmp() environment*/
- int sigcatch();
- int fd;
- char bbscall[7];
- int numdigis;
- char whotocon[7];
- char digi[5][7]; /*digipeater strings*/
- int sendindex,recvindex;
-
- confwd() /* connect to bbses and forward whatever goes to them*/
- {
- int gotonext();
- int callmatch;
-
- getnames(); /*get the names to forward to*/
-
- if((fd=open(fwdfile,O_RDONLY)) < 1){ /*open the forwarding file*/
- perror("confwd: Can't open forwarding file");
- return ;
- }
- for(;;){ /*main forwarding loop*/
- nullsndrec(); /*null out the send and receive line pointer arrays*/
-
- if(getfwdinfo() == 1) /*get forwarding info for one bbs */
- break; /*break if no more to do from file*/
-
- callmatch = false; /*preset call match off*/
- for(bbsindex=0;bbsindex < 15; bbsindex++){ /*let see if we have mail for this one*/
- if(!strcmp(bbsname[bbsindex],bbscall)){ /*does call match?*/
- callmatch = true;
- break;
- }
- }
- if(callmatch == false) /*keep looping if no mail for */
- continue; /* this bbs call gotten from file*/
- #ifdef DEBUG
- printf("bbs:found bbs match:beginning connect\n");
- #endif
- if(sendcon() == 1){ /*unsuccessful connect*/
- goto next;
- }
- sendindex=0; /*if all ok to this point*/
- recvindex=0; /*set send & recv indexes to 0*/
- signal(SIGUSR1,gotonext);
- if(setjmp(env) == -1){ /*disconnect signal received from*/
- #ifdef DEBUG /* the NET process */
- printf("bbs:signal SIGUSR received\n");
- #endif
- goto next;
- }
- conbbs(); /*continue connection to bbs*/
- #ifdef DEBUG
- printf("bbs:successful connection\n");
- #endif
- next:
- #ifdef DEBUG
- printf("bbs:sending N\n");
- #endif
- sprintf(prinbuf,"N\0"); /* tell NET to goto next con*/
- cprinout(); /*send control message*/
- signal(SIGUSR1,SIG_IGN);
- do{ /* wait for confirmation of 'goto next' from NET*/
- #ifdef DEBUG
- printf("calling cgetline , looking for N from NET\n");
- #endif
- cgetline();
- #ifdef DEBUG
- printf("bbs:got this from cgetline call:%s\n",inline);
- #endif
- }while(inline[0] != 'N');
- #ifdef DEBUG
- printf("bbs:N received from net, going to next\n");
- #endif
- }/*for, which is exited by break above */
- #ifdef DEBUG
- printf("bbs:sending K\n");
- #endif
- sprintf(prinbuf,"K\0"); /*done with all forwarding*/
- cprinout(); /*so kill bbs process*/
- for(;;); /*wait for kill*/
- } /* end of routine*/
-
-
- dofwd()
- {
-
- FILE *fp;
- char s[30];
- char hdr[128];
- char buf[128];
-
- int ic;
- int i,gotbytes;
- struct msghdr mhdr;
- int fd2;
- char *cp;
-
- sprintf(prinbuf,"ls %s | fgrep %s",fwddir,user.call); /*scan forwarding dir*/
- fp=popen(prinbuf,"r");
- for(;;){
- i=0;
- while(((ic = getc(fp)) != '\n') && (ic != EOF)) /*read file names*/
- s[i++] = ic & 0x7f;
- if(ic == EOF) break; /*break loop if EOF*/
- s[i] = '\0'; /*terminate filename*/
-
- sprintf(prinbuf,"%s%s",fwddir,s); /*file name in s*/
-
- if((fd2=open(prinbuf,O_RDONLY)) < 0){
- sprintf(prinbuf,"Cant open message file no.%s\n",s);
- perror(prinbuf);
- continue; /*shouldn't happen*/
- }
- else
- {
- read(fd2,hdr,86);
- conhdr(hdr,&mhdr); /*convert header to structure*/
-
- sprintf(buf,"S"); /*start with the "S" command*/
-
- if(strlen(mhdr.type) > 0){ /*if there is a type, send it*/
- sprintf(prinbuf,"%c",mhdr.type);/* as the option to s command*/
- strcat(buf,prinbuf);
- }
-
- sprintf(prinbuf," %s ",mhdr.tocall); /*append 'to' call*/
- strcat(buf,prinbuf);
-
- if(mhdr.atbbs[0] != '\0'){ /*append '@bbs' */
- sprintf(prinbuf,"@ %s ",mhdr.atbbs);
- strcat(buf,prinbuf);
- }
-
- sprintf(prinbuf,"< %s",mhdr.fromcall); /*append 'fromcall' */
- strcat(buf,prinbuf);
-
- if(mhdr.bid[0] != '\0')
- sprintf(prinbuf," $%s\n",mhdr.bid); /*append BID if any*/
- else
- sprintf(prinbuf,"\n\0"); /* else, just NL */
- strcat(buf,prinbuf);
-
- strcpy(prinbuf,buf); /* copy to output*/
- prinout(FLUSH);
-
- /*now we have sent the message header*/
- /* see if it is OK */
-
- getline(0); /* get OK or NO */
- if(revfwd){ /* if we dont get an OK, go to next */
- if(inline[0] != 'O'){
- goto skip; /* if not OK, consider it done*/
- }
- }
- sprintf(prinbuf,"%s\n",mhdr.title); /*send the title*/
- if(revfwd)
- prinout(NOFLUSH);
- else{
- prinout(FLUSH);
- getline(0);
- }
-
-
- /*make the path line*/
- sprintf(prinbuf,"R:%s/%sz @:%s %s O:%s\n",mhdr.dat,mhdr.tim,my.call,my.qth,mhdr.fromcall);
- prinout(NOFLUSH);
-
- while(gotbytes = read(fd2,prinbuf,80)){ /*send the file*/
- prinbuf[gotbytes] = '\0';
- cp = prinbuf;
- while(*cp){ /*change LF to CR*/
- if(*cp == '\n')
- *cp = '\r';
- cp++;
- }
- prinout(NOFLUSH);
- }
-
- prinbuf[0] = 0x1a;
- prinbuf[1] = '\r';
- prinbuf[2] = '\0';
- prinout(FLUSH);
- close(fd2); /* close the file(tidy, aren't we?) */
-
- skip: waitprompt(); /* wait for prompt from remote bbs*/
-
- sprintf(prinbuf,"%s%s",fwddir,s); /*file name in s*/
- unlink(prinbuf);
- }/*end of else-file-opened-OK */
- } /*end of for loop*/
- pclose(fp);
- if(fwding){
- return;
- }
- sndupkil(); /* and kill bbs process */
- }
-
- waitprompt() /*wait for a prompt*/
- {
- do{
- getline(0);
- }while(inline[strlen(inline)-1] != '>');
- }
-
- /*GETNAMES IS USED IN FORWARDING INITIATED BY *THIS* BBS*/
-
- getnames() /*fill bbs name array*/
- { /* containing names of bbses to*/
- /* which to initiate fwding connects*/
- int i,j,ic;
- FILE *fp;
- char s[30];
-
-
- sprintf(prinbuf,"ls %s\n",fwddir);
- fp=popen(prinbuf,"r"); /*open the pipe from ls*/
-
-
- for(;;){ /*main loop*/
- i=0;
- while(((ic = getc(fp)) != '\n') && (ic != EOF)) /*read message numbers*/
- s[i++] = ic & 0x7f;
- if(ic == EOF) break; /*break loop if EOF*/
- s[i] = '\0'; /*terminate filename*/
-
- for(j=0;j < strlen(s); j++){ /*shorten to just bbs name*/
- if(s[j] == 't'){
- s[j]='\0';
- break;
- }
- }
-
- for(j=0;j < 15; j++){
- if(!strcmp(bbsname[j],s)) /*if name is already there, exit loop*/
- break;
-
- if(bbsname[j][0]=='\0'){ /*if empty slot and name not used, */
- strcpy(bbsname[j],s); /* put it in the slot*/
- bbscount++; /* set the count of bbses up */
- break;
- }
- } /*for j=0 to 14 */
- }
- pclose(fp); /*close the pipe*/
- }
-
- devassign(dev) /*assign device name according to dev char*/
- char dev;
- { switch(dev){
- case 'A': strcpy(device,DEVICE1);
- break;
- #ifdef DEVICE2
- case 'B': strcpy(device,DEVICE2);
- break;
- #endif
- #ifdef DEVICE3
- case 'C': strcpy(device,DEVICE3);
- break;
- #endif
- #ifdef DEVICE4
- case 'D': strcpy(device,DEVICE4);
- break;
- #endif
- #ifdef DEVICE5
- case 'E': strcpy(device,DEVICE5);
- break;
- #endif
- default: strcpy(device,DEVICE1);
- }
-
- }
-
- gotonext()
- {
- longjmp(env,-1);
- }
-
- sigcatch(sig)
- int sig;
- {
- if(sig != 17) return;
- signal(SIGUSR2,SIG_IGN); /*ignore signal until next needed*/
- }
-
-
- nullsndrec()
- {
-
- /*null out the send and receive line pointer arrays*/
- for(sendindex=0;sendindex < 5; sendindex++)
- sendline[sendindex]=NULL;
- for(recvindex=0;recvindex < 5; recvindex++)
- recvline[recvindex]=NULL;
- }
-
- getfwdinfo() /*get forwarding info from fwd.xo file*/
- {
- char temp1[80],*p,*q;
- char temp2[80];
- int doneflag,i;
-
- doneflag = false;
- do{
- if(fgetline(fd,temp1) < 1) return 1;
- /*get a line from the forwarding file*/
- switch(temp1[0]){
- case 'G': /* if we get a 'G' line first */
- devassign(temp1[1]); /*assign device name by second char in line*/
- p=temp1; /*point to the line*/
- while((*p != ' ') && (*p != '\0')) /*toggle up to the space*/
- p++;
- while((*p == ' ') && (*p != '0')) /* and past any spaces*/
- p++;
- q=bbscall; /*point to the bbscall string*/
- while((*q = *p) != ' ' && *p != '\0'){
- p++;
- q++;
- }
- *q='\0'; /*terminate bbscall*/
- i=0; /*set up for first digi field*/
- numdigis = 0;
- if(*p != '\0'){ /* digi fields are there*/
- do{
- q=digi[i++];
- while(*p == ' ')/*toggle past any spaces*/
- p++;
- while((*q = *p) != ' ' && *p != '\0'){
- p++;
- q++;
- }
- *q='\0';
- }while(*p != '\0');
- }
- numdigis=i;
- doneflag=true; /* we're all done, no netrom stuff */
- break; /* exit the switch*/
- case 'C': /*this is a connect line first*/
- p = &temp1[3];
- q = whotocon;
- while((*q = *p) != ' ' && *p != '\0'){
- p++;
- q++;
- }
- *q='\0';
- numdigis = 0;
- sendindex=recvindex=0; /*initialize send and recv indexes*/
- do{ /*read up to the next 'G' line*/
- if((fgetline(fd,temp2)) < 1) /*get a line*/
- return 1;
- switch(temp2[0]){
- case 'S': sendline[sendindex]=(char *)malloc(60);
- strcpy(sendline[sendindex++],&temp2[1]);/*copy sendline*/
- break;
- case 'R': recvline[recvindex]=(char *)malloc(60);
- strcpy(recvline[recvindex++],&temp2[1]);/*copy sendline*/
- break;
- case 'G':
- devassign(temp2[1]); /*assign device name*/
- p=temp2; /*point to the line*/
- while(*p++ != ' ');/*toggle up to the space*/
- rdfield(p,bbscall);/*get bbs call from line */
- doneflag=true;
- do{
- if(fgetline(fd,temp2) < 1)
- return 1;
- }while(temp2[0] != '*');
- break;
- default: break;
- } /*switch on temp2[1] */
- }while((temp1[0] != 'G') && (!doneflag)); /*terminate when we get a G line*/
- default: break;
- } /*switch on temp1[1] */
- }while(!doneflag); /*done for this bbs */
- return 0; /*successful bbs read*/
- }
-
-
- conbbs()
- {
- /* complete the connection to bbs that we are*/
- /* going to forward to */
-
- for(;;){
- if(sendline[sendindex]==NULL) /* Any more to send?*/
- break;
- sprintf(prinbuf,"%s\n",sendline[sendindex++]);/*send send line*/
- prinout(FLUSH);
- if(recvline[recvindex] == NULL) /*if we need no more responses*/
- break;
- getline(1); /*if we are expecting response*/
- if(strcmp(inline,recvline[recvindex++])){/*error of some sort*/
- #ifdef DEBUG
- printf("bbs:conbbs:proper response not received\n");
- #endif
- return 1;
- }
-
- } /*for(;;)*/
-
- /* OK, we are done with the connect stuff*/
-
-
- strcpy(user.call,bbscall); /*make the current user the remote bbs*/
- user.typ='B'; /* and mark it as bbs */
-
- #ifdef DEBUG
- printf("Looking for prompt \n");
- #endif
- /*look for prompt*/
- do{
- getline(1); /*get a prompt from remote bbs*/
- #ifdef DEBUG
- printf("%s\n",inline);
- #endif
- if((inline[0] == '[')
- && (inline[strlen(inline)-1] == ']')){
- #ifdef DEBUG
- printf("SID Received\n");
- #endif
- revfwd = true; /*set reverse forward flag*/
- }
- }while(inline[strlen(inline)-1] != '>');
- #ifdef DEBUG
- if(revfwd) printf("Sending our SID\n");
- #endif
- /* if reverse forwarding bbs is connected, send SID*/
- /* and get second prompt*/
- if(revfwd){
- sprintf(prinbuf,"[XOBBS-V1.2-$]\n");/*send rev fwd sid*/
- prinout(FLUSH);
-
- #ifdef DEBUG
- printf("Looking for a prompt again\n");
- #endif
- do{
- getline(1);
- }while(inline[strlen(inline)-1] != '>'); /*get prompt*/
- #ifdef DEBUG
- printf("Prompt received after SID sent\n");
- #endif
- }
-
-
- dofwd(); /*forward to the remote bbs*/
- /* now, if we are connected to reverse forwarding bbs, do so*/
- if(revfwd){
- fwding=false; /*clear so bbs operates normally*/
- sprintf(prinbuf,"F>\n"); /*send reverse foward prompt*/
- prinout(FLUSH);
- /*now, act like incoming bbs for a while*/
- for(;;){
- getline(0); /*get a line*/
- if(matchn2("*** ",inline,4)){ /*check for *** Done*/
- fwding = true;
- break;
- }
- else{
- if(parse()){ /*parse line*/
- docmd(); /* do what is asked*/
- sprintf(prinbuf,">\n"); /*print a prompt*/
- prinout(FLUSH);
- }
- else{
- fwding = true;
- break; /*something screwed up*/
- }
- }
- /* so drop the connection*/
- }/*for*/
- fwding = true;
- }/*if reverse forward*/
- return 0;
- }
-
- sendcon() /*send connect info to net */
- {
- int i;
-
- sprintf(prinbuf,"%s",device); /*device name*/
- prinout(FLUSH);
- sprintf(prinbuf,"%s",whotocon); /*call letters*/
- prinout(FLUSH);
- sprintf(prinbuf,"%d",numdigis); /*number of digis*/
- prinout(FLUSH);
- for(i=0;i<numdigis;i++){ /*digi calls*/
- sprintf(prinbuf,"%s",digi[i]);
- prinout(FLUSH);
- }
- prinbuf[0] = 0x04; /*control D for termination*/
- prinbuf[1] = '\0';
- prinout(FLUSH);
- /* connect info now sent, get a line from the bbs */
- getline(0);
-
- if(indexx("NOGOOD",inline) >= 0){ /*Busy messsage? */
- bbsname[bbsindex][0]='\0'; /*no more for this bbs this time*/
- return 1; /*return, no success */
- }
- return 0; /*successful connect*/
- }
-
-